package org.hxyjs.service.impl;

import org.hxyjs.dao.CheforderDao;
import org.hxyjs.dto.DTO;
import org.hxyjs.dto.MessageEnum;
import org.hxyjs.entity.Cheforder;
import org.hxyjs.page.PageInfo;
import org.hxyjs.service.CheforderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Service("CheforderService")
public class CheforderServiceImpl implements CheforderService {
    @Resource
    CheforderDao cheforderDao;
    @Override
    public DTO<Cheforder> getAllCheforders()  throws Exception{
        List<Cheforder> cheforders = cheforderDao.getAllCheforders();
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  = Optional.ofNullable(cheforders).isPresent()?MessageEnum.GETADMINSUCCESS:MessageEnum.GETADMINFAIL;
        return new DTO<Cheforder>(messageEnum.getCode(),messageEnum.getMessage(),cheforders);
    }

    @Override
    public DTO<Cheforder> deleteCheforderByCuno(String cuno)  throws Exception{
        Integer result = cheforderDao.deleteCheforderByCuno(cuno);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  =result> 0?MessageEnum.DELETEADMINSUCCESS:MessageEnum.DELETEADMINFAIL;
        return new DTO<Cheforder>(messageEnum.getCode(),messageEnum.getMessage());
    }
    @Override
    public DTO<Cheforder> getCheforderByCuno(String cuno)  throws Exception{
        Cheforder cheforders = cheforderDao.getCheforderByCuno(cuno);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  = Optional.ofNullable(cheforders).isPresent()?MessageEnum.GETADMINSUCCESS:MessageEnum.GETADMINFAIL;
        return new DTO<Cheforder>(messageEnum.getCode(),messageEnum.getMessage(),cheforders);
    }

    @Override
    public DTO<Cheforder> updateCheforderByCuno(Cheforder cheforder) throws Exception {
        Integer result = cheforderDao.updateCheforderByCuno(cheforder);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  =result> 0?MessageEnum.UPDATEADMINSUCCESS:MessageEnum.UPDATEADMINFAIL;
        return new DTO<Cheforder>(messageEnum.getCode(),messageEnum.getMessage());
    }

    @Override
    public DTO<Cheforder> addCheforder(Cheforder cheforder) throws Exception {
        Integer result = cheforderDao.addCheforder(cheforder);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  =result> 0?MessageEnum.ADDADMINSUCCESS:MessageEnum.ADDADMINFAIL;
        return new DTO<Cheforder>(messageEnum.getCode(),messageEnum.getMessage());
    }

    /**
     * 学生选课成功 学生成绩加1分，从选课信息中可以获得学生的学号就能查询出学生原来的成绩
     * @param
     * @return
     * @throws Exception
     */
    @Override
    public DTO<Cheforder> getCheforderByPage(PageInfo pageInfo) throws Exception {
        List<Cheforder> cheforders = cheforderDao.getCheforderByPage(pageInfo);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  = Optional.ofNullable(cheforders).isPresent()?MessageEnum.GETADMINSUCCESS:MessageEnum.GETADMINFAIL;
        return new DTO<Cheforder>(messageEnum.getCode(),messageEnum.getMessage(),cheforders,pageInfo);
    }

}
